package com.bestdo.t;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Consumer;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class PingYin {
	public static void main(String[] args) throws IOException {
		System.out.println(getPingYin("7天教你搞定熊孩子的哭和闹"));
	}

	public static void main1(String[] args) throws IOException {
		String date = "20160718";
		Path path = Paths.get("/Users/zhaokuner/Documents/yzmm/三千问/" + date);
		Files.newDirectoryStream(path).forEach(new Consumer<Path>() {
			@Override
			public void accept(Path t) {
				if (!t.getFileName().toString().endsWith("mp3")) {
					return;
				}
				String name = t.getFileName().toString().replaceAll("\\.mp3", "");
				String forpinyin = name.trim().replaceAll("[(\\.mp3)“”， ]", "");
				String pinyin = getPingYin(forpinyin);
				pinyin = date + pinyin;
				System.out.println(name + " " + pinyin + ".mp3");
				try {
					Files.move(t, Paths.get(t.toString().replace(name, pinyin)));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static void main2(String[] args) throws IOException {
		String date = "20160719";
		Path path = Paths.get("/Users/zhaokuner/Documents/yzmm/产品迭代/C端/20160718每日一喵/20160719三千问图");
		Files.newDirectoryStream(path).forEach(new Consumer<Path>() {
			@Override
			public void accept(Path t) {
				if (!t.getFileName().toString().endsWith("jpg")) {
					return;
				}
				String name = t.getFileName().toString().replaceAll("\\.jpg", "");
				String forpinyin = name.trim().replaceAll("[(\\.jpg)“”？\\?， ]", "");
				String pinyin = getPingYin(forpinyin);
				// pinyin = date + pinyin;
				System.out.println(name + " " + pinyin + ".jpg");
				try {
					Files.move(t, Paths.get(t.toString().replace(name, pinyin)));
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
	}

	// 将汉字转换为全拼
	public static String getPingYin(String src) {

		char[] t1 = null;
		t1 = src.toCharArray();
		String[] t2 = new String[t1.length];
		HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();

		t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);
		t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
		t3.setVCharType(HanyuPinyinVCharType.WITH_V);
		String t4 = "";
		int t0 = t1.length;
		try {
			for (int i = 0; i < t0; i++) {
				// 判断是否为汉字字符
				if (java.lang.Character.toString(t1[i]).matches("[\\u4E00-\\u9FA5]+")) {
					t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);
					t4 += t2[0];
				} else
					t4 += java.lang.Character.toString(t1[i]);
			}
			// System.out.println(t4);
			return t4;
		} catch (BadHanyuPinyinOutputFormatCombination e1) {
			e1.printStackTrace();
		}
		return t4;
	}

}
